package com.bw.dwd;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bw.MyKafkaUtil;
import com.bw.fun.HbaseSinkFunc;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

public class Test3 {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env=StreamExecutionEnvironment.createLocalEnvironment();
        env.setParallelism(1);

        DataStream<String> stream = env.addSource(MyKafkaUtil.getKafkaConsumer("ods_base_topic_yk8", "test" + System.currentTimeMillis()));



        OutputTag<String> dimTag=new OutputTag<String>("dim_table_source"){};

        SingleOutputStreamOperator<String> orderDS=stream.process(
                new ProcessFunction<String, String>() {

                    @Override
                    public void processElement(String s, ProcessFunction<String, String>.Context context, Collector<String> collector) throws Exception {
                        JSONObject ob= JSON.parseObject(s);

                        if(ob!=null)
                        {
                            String table=ob.getString("table");
                            String dim_tables="region,shop,product_sku,product_spu";
                            if(dim_tables.contains(table))
                            {
                                context.output(dimTag,ob.toJSONString());
                            }else
                                collector.collect(ob.toJSONString());
                        }
                    }
                }
        );


        DataStream<String> dimStream = orderDS.getSideOutput(dimTag);

        dimStream.addSink(new HbaseSinkFunc());

        env.execute();
    }
}
